﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Gemli.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests.Gemli.Data
{
    [TestClass]
    public class DataModelFieldMappingAttributeTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        public class MockDataModel : DataModel
        {
            [DataModelFieldMapping]
            public string Field
            {
                get { return (string)base["Field"]; }
                set { base["Field"] = value; }
            }
        }

        public class MockDataModel2 : DataModel
        {
            [DataModelFieldMapping]
            public string Field2
            {
                get { return (string)base["Field"]; }
                set { base["Field"] = value; }
            }
        }

        public class MockDataModel3 : DataModel
        {
            [DataModelFieldMapping("00ga")]
            public virtual string Field3
            {
                get { return (string)base["00ga"]; }
                set { base["00ga"] = value; }
            }
        }

        public class MockDataModel4 : MockDataModel3
        {
        }

        public class MockDataModel5 : MockDataModel4
        {
            [DataModelFieldMapping("00ga2", ClearBaseObjectMapping = true)]
            public override string Field3
            {
                get { return (string)base["00ga"]; }
                set { base["00ga"] = value; }
            }
        }

        [TestMethod]
        public void DataModelFieldMappingAttribute_DefaultNameTest()
        {
            var target = new MockDataModel();
            Assert.AreEqual(target.EntityMappings.FieldMappings["Field"].ColumnName, "Field");
        }

        [TestMethod]
        public void DataModelFieldMappingAttribute_DefaultNameTest2()
        {
            var target = new MockDataModel2();
            Assert.AreEqual(target.EntityMappings.FieldMappings["Field2"].ColumnName, "Field2");
        }

        [TestMethod]
        public void DataModelFieldMappingAttribute_SetNameTest()
        {
            var target = new MockDataModel3();
            Assert.AreEqual(target.EntityMappings.FieldMappings["Field3"].ColumnName, "00ga");
        }

        [TestMethod]
        public void DataModelFieldMappingAttribute_InheritAttribsTest()
        {
            var target = new MockDataModel4();
            Assert.AreEqual(target.EntityMappings.FieldMappings["Field3"].ColumnName, "00ga");
        }

        [TestMethod]
        public void DataModelFieldMappingAttribute_ClearInheritAttribsTest()
        {
            var target = new MockDataModel5();
            Assert.AreEqual(target.EntityMappings.FieldMappings["Field3"].ColumnName, "00ga2");
            Assert.IsFalse(target.EntityMappings.FieldMappings.ContainsKey("00ga"));
        }

        [TestMethod]
        public void DataModelFieldMappingAttribute_MemberAssignedTest()
        {
            var target = new MockDataModel5();
            var fieldmap = target.EntityMappings.FieldMappings["Field3"];
            Assert.IsNotNull(fieldmap.TargetMember);
        }
    }
}